package com.bwie.service.impl;

import com.bwie.config.RedisConfig;
import com.bwie.mapper.LoginMapper;
import com.bwie.pojo.Result;
import com.bwie.pojo.dto.UserDto;
import com.bwie.pojo.po.Mune;
import com.bwie.pojo.po.User;
import com.bwie.pojo.vo.MuneVo;
import com.bwie.pojo.vo.UserVo;
import com.bwie.service.LoginService;
import com.bwie.utils.AesUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private HttpSession session;
    @Override
    public Result login(UserDto userDto) throws Exception {

        //开启redis
        JedisPool jedisPool = RedisConfig.redisConn();
        Jedis resource = jedisPool.getResource();

        //记录失败次数
        String errorCountKey="ERRORCOUNT"+userDto.getUsername();

        //记录禁用
        String lockKet = "LOCK"+userDto.getUsername();


        //如果禁用记录不是空（禁用状态）
        String lockStr = resource.get(lockKet);
        if (StringUtils.isNotEmpty(lockStr)){
            return Result.error("错误次数过多，请3分钟后再试");
        }

        //如果失败次数大于3则禁用3分钟
        String errorCountStr = resource.get(errorCountKey);
        if (StringUtils.isNotEmpty(errorCountStr)){
            Integer i = Integer.valueOf(errorCountStr);
            if (i>=3){
                resource.setex(lockKet,3*60,"aaaaaa");
                return Result.error("错误次数过多，禁用3分钟");
            }
        }


        User user = loginMapper.login(userDto);
        if (null==user){
            return Result.error("用户名不存在");
        }
        String password = userDto.getPassword();
        String pwd = AesUtil.decrypt(password);
        if (!pwd.equals(user.getPassword())){
            //失败记录次数
            resource.incr(errorCountKey);
            resource.expire(errorCountKey,2*60);


            return Result.error("密码错误");
        }


        List<Integer> roleList=loginMapper.getRoleList(user.getUserId());
        String roleListStr=",";

        for (Integer roleId : roleList) {
            roleListStr=roleListStr+roleId+",";
        }

        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        userVo.setRoleListStr(roleListStr);

        session.setAttribute("user",userVo);
        return Result.success();
    }

    /**
     * 侧边栏
     * @return
     */
    @Override
    public List<MuneVo> getMuneVo() {
        User user = (User) session.getAttribute("user");
        List<Mune> muneList= loginMapper.getMuneVo(user.getUserId());

        return addMuneVo(muneList,0);
    }

    private List<MuneVo> addMuneVo(List<Mune> muneList, Integer muneId) {
        List<MuneVo> muneVoList = new ArrayList<>();
        for (Mune mune : muneList) {
            if (muneId==0?mune.getMuneParentId()==0:muneId.equals(mune.getMuneParentId())){
                MuneVo muneVo = new MuneVo();
                BeanUtils.copyProperties(mune,muneVo);
                muneVo.setMuneVoList(addMuneVo(muneList,mune.getMuneId()));
                muneVoList.add(muneVo);
            }
        }

        return muneVoList;
    }
}
